﻿using Basic.WxApiRemoteModel;
using Basic.WxModel.DB;
using Basic.WxModel.WxMsgFilter;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.WxRepository.lmpl
{
    internal class WxMsgFilterRepository : WxBasicRepository<DBWxMsgSubscribeFilter, long>, IWxMsgFilterRepository
    {
        public WxMsgFilterRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public DBWxMsgSubscribeFilter[] GetsBySubscribeId (long subscribeId)
        {
            return base.Gets(c => c.SubscribeId == subscribeId);
        }
        public void Set (WxMsgFilterSet set)
        {
            ISqlQueue<DBWxMsgSubscribeFilter> queue = base.BeginQueue();
            if (!set.DeleteId.IsNull())
            {
                queue.Delete(c => set.DeleteId.Contains(c.Id));
            }
            DateTime now = DateTime.Now;
            long timeSpan = now.ToLong();
            if (!set.Adds.IsNull())
            {
                queue.Insert(set.Adds.ConvertAll(c =>
                {
                    return new DBWxMsgSubscribeFilter
                    {
                        AddTime = now,
                        Id = IdentityHelper.CreateId(),
                        Direct = c.Direct,
                        SubscribeId = set.SubscribeId,
                        AppId = c.AppId,
                        FilterType = c.FilterType,
                        ForwardType = c.ForwardType,
                        LastUpdateTime = timeSpan,
                        MsgType = c.MsgType,
                        Reply = c.Reply,
                        WxAppId = c.WxAppId
                    };
                }));
            }
            if (set.Update.Count > 0)
            {
                DBWxMsgSubscribeFilter[] sets = set.Update.Select(c =>
                {
                    return new DBWxMsgSubscribeFilter
                    {
                        Id = c.Id,
                        Direct = c.Direct,
                        SubscribeId = set.SubscribeId,
                        ForwardType = c.ForwardType,
                        LastUpdateTime = timeSpan,
                        Reply = c.Reply
                    };
                }).ToArray();
                queue.Update(sets, new string[]
                {
                    "Direct",
                    "SubscribeId",
                    "ForwardType",
                    "LastUpdateTime",
                    "Reply"
                });
            }

            _ = queue.Submit();
        }
        public T[] Gets<T> (WxMsgFilterType filterType) where T : class, new()
        {
            return base.Gets<T>(a => a.FilterType == filterType);
        }
        public WxMsgFilterDto[] GetFullFilter (long[] ids)
        {
            return base.Join<DBWxMsgSubscribe, WxMsgFilterDto>((a, b) => a.SubscribeId == b.Id && ids.Contains(a.Id), (a, b) => new WxMsgFilterDto
            {
                AppId = a.AppId,
                Direct = a.Direct,
                WxAppId = a.WxAppId,
                ForwardType = a.ForwardType,
                Id = a.Id,
                LastUpdateTime = a.LastUpdateTime,
                MsgType = a.MsgType,
                Reply = a.Reply,
                SystemType = b.SystemType,
                RpcMerId = b.RpcMerId,
            });
        }
    }
}
